//
//  MainMenu.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2019/12/17.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

import Cocoa

// Apparently these aren't declared anywhere
@objc protocol EditMenuActions {
    func redo(_ sender:AnyObject)
    func undo(_ sender:AnyObject)
}

extension AppDelegate {
    func populateMainMenu() {
        let mainMenu = NSMenu(title:"MainMenu")
        
        // The titles of the menu items are for identification purposes only and shouldn't be localized.
        // The strings in the menu bar come from the submenu titles,
        // except for the application menu, whose title is ignored at runtime.
        var menuItem = mainMenu.addItem(withTitle:"Application", action:nil, keyEquivalent:"")
        var submenu = NSMenu(title:"Application")
        populateApplicationMenu(submenu)
        mainMenu.setSubmenu(submenu, for:menuItem)
        
        menuItem = mainMenu.addItem(withTitle:"Edit", action:nil, keyEquivalent:"")
        submenu = NSMenu(title:NSLocalizedString("Edit", comment:"Edit menu"))
        populateEditMenu(submenu)
        mainMenu.setSubmenu(submenu, for:menuItem)
        
        menuItem = mainMenu.addItem(withTitle:"Window", action:nil, keyEquivalent:"")
        submenu = NSMenu(title:NSLocalizedString("Window", comment:"Window menu"))
        populateWindowMenu(submenu)
        mainMenu.setSubmenu(submenu, for:menuItem)
        NSApp.windowsMenu = submenu
        
        menuItem = mainMenu.addItem(withTitle:"Help", action:nil, keyEquivalent:"")
        submenu = NSMenu(title:NSLocalizedString("Help", comment:"Help menu"))
        populateHelpMenu(submenu)
        mainMenu.setSubmenu(submenu, for:menuItem)
        
        NSApp.mainMenu = mainMenu
    }
    
    private func populateApplicationMenu(_ menu:NSMenu) {
        var title = NSLocalizedString("About", comment:"About menu item") + " " + applicationName
        var menuItem = menu.addItem(withTitle:title, action:#selector(NSApplication.orderFrontStandardAboutPanel(_:)), keyEquivalent:"")
        menuItem.target = NSApp
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Services", comment:"Services menu item")
        menuItem = menu.addItem(withTitle:title, action:nil, keyEquivalent:"")
        let servicesMenu = NSMenu(title:"Services")
        menu.setSubmenu(servicesMenu, for:menuItem)
        NSApp.servicesMenu = servicesMenu
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Hide", comment:"Hide menu item") + " " + applicationName
        menuItem = menu.addItem(withTitle:title, action:#selector(NSApplication.hide(_:)), keyEquivalent:"h")
        menuItem.target = NSApp
        
        title = NSLocalizedString("Hide Others", comment:"Hide Others menu item")
        menuItem = menu.addItem(withTitle:title, action:#selector(NSApplication.hideOtherApplications(_:)), keyEquivalent:"h")
        menuItem.keyEquivalentModifierMask = [.command, .option]
        menuItem.target = NSApp
        
        title = NSLocalizedString("Show All", comment:"Show All menu item")
        menuItem = menu.addItem(withTitle:title, action:#selector(NSApplication.unhideAllApplications(_:)), keyEquivalent:"")
        menuItem.target = NSApp
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Quit", comment:"Quit menu item") + " " + applicationName
        menuItem = menu.addItem(withTitle:title, action:#selector(NSApplication.terminate(_:)), keyEquivalent:"q")
        menuItem.target = NSApp
    }
    
    private func populateEditMenu(_ menu:NSMenu) {
        var title = NSLocalizedString("Undo", comment:"Undo menu item")
        menu.addItem(withTitle:title, action:#selector(EditMenuActions.undo(_:)), keyEquivalent:"z")
        
        title = NSLocalizedString("Redo", comment:"Redo menu item")
        menu.addItem(withTitle:title, action:#selector(EditMenuActions.redo(_:)), keyEquivalent:"Z")
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Cut", comment:"Cut menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.cut(_:)), keyEquivalent:"x")
        
        title = NSLocalizedString("Copy", comment:"Copy menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.copy(_:)), keyEquivalent:"c")
        
        title = NSLocalizedString("Paste", comment:"Paste menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.paste(_:)), keyEquivalent:"v")
        
        title = NSLocalizedString("Paste and Match Style", comment:"Paste and Match Style menu item")
        var menuItem = menu.addItem(withTitle:title, action:#selector(NSTextView.pasteAsPlainText(_:)), keyEquivalent:"V")
        menuItem.keyEquivalentModifierMask = [.command, .option]
        
        title = NSLocalizedString("Delete", comment:"Delete menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.delete(_:)), keyEquivalent:"\u{8}") // backspace
        
        title = NSLocalizedString("Select All", comment:"Select All menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.selectAll(_:)), keyEquivalent:"a")
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Find", comment:"Find menu item")
        menuItem = menu.addItem(withTitle:title, action:nil, keyEquivalent:"")
        let findMenu = NSMenu(title:"Find")
        populateFindMenu(findMenu)
        menu.setSubmenu(findMenu, for:menuItem)
        
        title = NSLocalizedString("Spelling", comment:"Spelling menu item")
        menuItem = menu.addItem(withTitle:title, action:nil, keyEquivalent:"")
        let spellingMenu = NSMenu(title:"Spelling")
        populateSpellingMenu(spellingMenu)
        menu.setSubmenu(spellingMenu, for:menuItem)
    }
    
    private func populateFindMenu(_ menu:NSMenu) {
        var title = NSLocalizedString("Find…", comment:"Find… menu item")
        var menuItem = menu.addItem(withTitle:title, action:#selector(NSResponder.performTextFinderAction(_:)), keyEquivalent:"f")
        menuItem.tag = NSTextFinder.Action.showFindInterface.rawValue
        
        title = NSLocalizedString("Find Next", comment:"Find Next menu item")
        menuItem = menu.addItem(withTitle:title, action:#selector(NSResponder.performTextFinderAction(_:)), keyEquivalent:"g")
        menuItem.tag = NSTextFinder.Action.nextMatch.rawValue
        
        title = NSLocalizedString("Find Previous", comment:"Find Previous menu item")
        menuItem = menu.addItem(withTitle:title, action:#selector(NSResponder.performTextFinderAction(_:)), keyEquivalent:"G")
        menuItem.tag = NSTextFinder.Action.previousMatch.rawValue
        
        title = NSLocalizedString("Use Selection for Find", comment:"Use Selection for Find menu item")
        menuItem = menu.addItem(withTitle:title, action:#selector(NSResponder.performTextFinderAction(_:)), keyEquivalent:"e")
        menuItem.tag = NSTextFinder.Action.setSearchString.rawValue
        
        title = NSLocalizedString("Jump to Selection", comment:"Jump to Selection menu item")
        menu.addItem(withTitle:title, action:#selector(NSResponder.centerSelectionInVisibleArea(_:)), keyEquivalent:"j")
    }
    
    private func populateSpellingMenu(_ menu:NSMenu) {
        var title = NSLocalizedString("Spelling…", comment:"Spelling… menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.showGuessPanel(_:)), keyEquivalent:":")
        
        title = NSLocalizedString("Check Spelling", comment:"Check Spelling menu item")
        menu.addItem(withTitle:title, action:#selector(NSText.checkSpelling(_:)), keyEquivalent:";")
        
        title = NSLocalizedString("Check Spelling as You Type", comment:"Check Spelling as You Type menu item")
        menu.addItem(withTitle:title, action:#selector(NSTextView.toggleContinuousSpellChecking(_:)), keyEquivalent:"")
    }
    
    private func populateWindowMenu(_ menu:NSMenu) {
        var title = NSLocalizedString("Close", comment:"Close Window menu item")
        
        menu.addItem(withTitle:title, action:#selector(NSWindow.performClose(_:)), keyEquivalent:"w")
        title = NSLocalizedString("Minimize", comment:"Minimize menu item")
        menu.addItem(withTitle:title, action:#selector(NSWindow.performMiniaturize(_:)), keyEquivalent:"m")
        
        title = NSLocalizedString("Zoom", comment:"Zoom menu item")
        menu.addItem(withTitle:title, action:#selector(NSWindow.performZoom(_:)), keyEquivalent:"")
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Enter Full Screen", comment:"Enter Full Screen menu item")
        var menuItem = menu.addItem(withTitle:title, action:#selector(NSWindow.toggleFullScreen(_:)), keyEquivalent:"f")
        menuItem.keyEquivalentModifierMask = [.command, .control]
        
        menu.addItem(NSMenuItem.separator())
        
        title = NSLocalizedString("Bring All to Front", comment:"Bring All to Front menu item")
        menuItem = menu.addItem(withTitle:title, action:#selector(NSApplication.arrangeInFront(_:)), keyEquivalent:"")
        menuItem.target = NSApp
    }
    
    private func populateHelpMenu(_ menu:NSMenu) {
        var title = applicationName + " " + NSLocalizedString("Web Site", comment:"Web Site")
        var menuItem = menu.addItem(withTitle:title, action:#selector(AppDelegate.openWebSite(_:)), keyEquivalent:"")
        menuItem.target = NSApp.delegate
        
        title = NSLocalizedString("License", comment:"License for the app")
        menuItem = menu.addItem(withTitle:title, action:#selector(AppDelegate.openLicense(_:)), keyEquivalent:"")
        menuItem.target = NSApp.delegate
    }
}
